package top.lgd.controller.operation;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lgd.base.LgdConstants;
import top.lgd.base.LgdTable;
import top.lgd.controller.LgdController;
import top.lgd.enums.AccontFlowTypeEnum;
import top.lgd.pojo.entity.operation.MemberFlow;
import top.lgd.pojo.res.operation.MemberFlowBriefRes;
import top.lgd.service.operation.MemberFlowService;
import top.lgd.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/19 19:12
 */
@RestController
@RequestMapping("/admin/operationMemberFlow")
public class MemberFlowAdminController extends LgdController {

    @Autowired
    private MemberFlowService memberFlowService;

    /**
     * 查询 会员流水[简介] 列表
     *
     * @param memberFlow 账户表查询条件
     * @return
     */
    @GetMapping("queryBriefList")
    public LgdTable<MemberFlowBriefRes> queryBriefList(MemberFlow memberFlow) {
        QueryWrapper<MemberFlow> queryWrapper = new QueryWrapper(memberFlow);
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .orderByDesc(LgdConstants.COLUMN_ID);
        Page<MemberFlow> pageInfo = memberFlowService.page(new Page(memberFlow.getCurrent(), memberFlow.getPageSize()), queryWrapper);
        List<MemberFlowBriefRes> dataList = new ArrayList<>();
        pageInfo.getRecords().forEach(f -> {
            MemberFlowBriefRes accountFlowBriefRes = new MemberFlowBriefRes();
            String brief = f.getFlowDate().toString();
            if (AccontFlowTypeEnum.IN.name().equals(f.getFlowType())) {
                brief += " (充值)：" + f.getBeforeAmount() + " + [" + f.getFlowAmount() + "] = " + f.getAfterAmount();
            } else {
                brief += " (消费)：" + f.getBeforeAmount() + " - [" + f.getFlowAmount() + " ] = " + f.getAfterAmount();
            }
            accountFlowBriefRes.setBrief(brief);
            accountFlowBriefRes.setBizDesc(LocalDateTimeUtil.formatNormal(f.getUpdateTime()) + " " +f.getRemark());
            dataList.add(accountFlowBriefRes);
        });
        return getTable(pageInfo, dataList);
    }
}